//# COMP 355    #  WONG YI CHUN        07017375         eg_wycaa
package com.jimmyapp.papa.game.logic;

import java.util.Stack;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

import com.jimmyapp.papa.game.GameListener;
import com.jimmyapp.papa.game.model.Box;



public class BoxGameLogic implements GameLogic{
	
	
	private int score = 0;
	private Stack<Box> sticks;
	public BoxGameLogic() {
		sticks = new Stack<Box>();
		initArray();
	}
	private int boxNum = 5;
	public void initArray() {
		int level = -3;
		float offset = 0.5f;
		boxNum = Math.min(5 + score/15, 10);
		sticks.clear();
		for(int i=boxNum-1;i>=0;i--) {
			sticks.push(new Box());
			xt[i] = (float)(Math.random()*4-2f)*offset;
			yt[i] = (float)(Math.random()*4-2f)*offset;
			zt[i] = (float)Math.random()+level*2;
			xr[i] = (float)Math.random()*4-2f;
			yr[i] = (float)Math.random()*4-2f;
			zr[i] = (float)Math.random();
			ar[i] = (float)Math.random()*180;
			level -= 1*Math.random()+1;
			offset += 0.2f;
		}
	}
	private float[] xt = new float[10];
	private float[] yt = new float[10];
	private float[] zt = new float[10];
	private float[] xr = new float[10];
	private float[] yr = new float[10];
	private float[] zr = new float[10];
	private float[] ar = new float[10];
	
	public void draw(GL10 gl) {
		Box[] boxs = sticks.toArray(new Box[0]);
		for (int i=0;i<boxs.length;i++) {
			gl.glPushMatrix();
			gl.glTranslatef(xt[i],yt[i],zt[i]);
			gl.glRotatef(ar[i], xr[i], yr[i], zr[i]);
			boxs[i].draw(gl);
			gl.glPopMatrix();
		}
	}
	public boolean pick(int color) {
		if (sticks.lastElement().color == color) {
			sticks.pop();
			score++;
			if (sticks.empty()) {
				initArray();
				score += 5;
			}
			return true;
		}
		return false;
	}
	@Override
	public int getScore() {
		return score;
	}
	@Override
	public void resetScore() {
		score = 0;
	}
	@Override
	public void run() {
		
	}
	@Override
	public void addGameListener(GameListener gameListener) {
		
	}
	@Override
	public void stopGame() {
		
	}
	@Override
	public void addView(GLSurfaceView glView) {
		// TODO Auto-generated method stub
		
	}
	
	
}
